package com.cloud.mall.oms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.oms.input.OmsOrderReturnReasonInpDTO;
import com.cloud.mall.oms.mapper.OmsOrderReturnReasonMapper;
import com.cloud.mall.oms.model.OmsOrderReturnReason;
import com.cloud.mall.oms.model.OmsOrderReturnReasonExample;
import com.cloud.mall.oms.out.OmsOrderReturnReasonOut;
import com.cloud.mall.oms.service.OmsOrderReturnReasonService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService {

    @Autowired
    private OmsOrderReturnReasonMapper returnReasonMapper;

    @Override
    public BaseResponse create(@RequestBody OmsOrderReturnReasonInpDTO returnReason) {
        OmsOrderReturnReason orderSetting=MallBeanUtils.dtoToDo(returnReason,OmsOrderReturnReason.class);
        returnReason.setCreateTime(new Date());
        int count= returnReasonMapper.insert(orderSetting);
        if (count==0){
            return  BaseResponse.setResultError();
        }
        return  BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse update(Long id,@RequestBody OmsOrderReturnReasonInpDTO returnReason) {
        OmsOrderReturnReason orderSetting=MallBeanUtils.dtoToDo(returnReason,OmsOrderReturnReason.class);
        returnReason.setId(id);
        int count= returnReasonMapper.updateByPrimaryKey(orderSetting);
        if (count==0){
            return  BaseResponse.setResultError();
        }
        return  BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<OmsOrderReturnReasonOut> getItem(Long id) {
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(returnReasonMapper.selectByPrimaryKey(id), OmsOrderReturnReasonOut.class));

    }

    @Override
    public BaseResponse delete(List<Long> ids) {
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return BaseResponse.setResultSuccess(returnReasonMapper.deleteByExample(example));
    }

    @Override
    public BaseResponse updateStatus(Integer status, List<Long> ids) {
        if(!status.equals(0)&&!status.equals(1)){
            return  BaseResponse.setResultError();
        }
        OmsOrderReturnReason record = new OmsOrderReturnReason();
        record.setStatus(status);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        int count= returnReasonMapper.updateByExampleSelective(record,example);
        if (count==0){
            return  BaseResponse.setResultError();
        }
        return  BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<CommonPage<OmsOrderReturnReasonOut>> list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.setOrderByClause("sort desc");
        List<OmsOrderReturnReason> reasonList= returnReasonMapper.selectByExample(example);
        return  BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(reasonList,OmsOrderReturnReasonOut.class)));
    }
}
